package view
{
	import assets.view.ProgressViewSymbol;
	
	import flash.display.BlendMode;
	import flash.display.Graphics;
	import flash.events.Event;
	import flash.filters.DropShadowFilter;
	import flash.geom.Vector3D;
	import flash.text.TextFieldAutoSize;
	
	import model.vo.ViewVO;
	
	import mx.core.TextFieldAsset;

	public class ProgressView extends ProgressViewSymbol
	{
		public static const NAME:String 				= "ProgressView";
		public static const SHOW:String					= NAME + "Show";
		public static const HIDE:String					= NAME + "Hide";
		public static const READY:String				= NAME + "Ready";
		
		private static const LABEL_TEXT:String			= "Loading ";
		
		private var progressData:XML;
		
		private var progressViewWidth:Number;
		private var progressViewHeight:Number;
		private var frameColor:Number;
		private var frameWeight:Number;
		private var bckgrdColor:Number;
		
		private var viewData:ViewVO;
		
		private var progressViewVecArt:Vector.<Vector3D>;
		
		public function ProgressView()
		{
		}
		
		public function init( viewVO:ViewVO ):void
		{
			viewData = viewVO;
			
			setProgressViewProperties();
			buildProgressView();
		}
		
		public function show( txt:String ):void
		{
			label.text = txt;
			label.x = -Math.round( label.width + 5 );
			this.addEventListener( Event.ENTER_FRAME, loop );
			FX.fadeIn( progressContainer );
			FX.fadeIn( label );
		}
		
		public function hide( txt:String  ):void
		{
			label.text = txt;
			label.x = -Math.round( label.width + 5 );
//			FX.fadeOut( bckgrd );
//			FX.fadeOut( scrollContainer );
			FX.fadeOutInterval( [ progressContainer, label ], 2 );
		}
		
		private function setProgressViewProperties():void
		{
			progressViewWidth 	= viewData.PROGRESS_WIDTH;
			progressViewHeight 	= viewData.PROGRESS_HEIGHT;
			
			frameColor 			= viewData.LINE_COLOR;
			frameWeight			= viewData.LINE_WEIGHT;
			
			bckgrdColor			= viewData.PROGRESS_BCKGRD_COLOR;
		}
		
		private function buildProgressView():void
		{
			paintBckgrd();
			paintScrollContainer();
			paintMask();
			paintFrame();
			
			label.autoSize = TextFieldAutoSize.LEFT;
			label.text = "";
			label.x = -Math.round( label.width + 5 );
			label.alpha = 0;
			
			progressContainer.alpha = 0;
			progressContainer.addChild( bckgrd );
			progressContainer.addChild( scrollContainer );
			progressContainer.addChild( scrollMask );
			scrollContainer.mask = scrollMask;
		}
		
		private function paintBckgrd():void
		{
			var g:Graphics = bckgrd.graphics;
			g.clear();
			g.beginFill( bckgrdColor, 1 );
			g.drawRect( 0, 0, progressViewWidth, progressViewHeight );
			g.endFill();
			
			bckgrd.filters = [ new DropShadowFilter(2,45,0x000000,1,4,4,1,1,true) ];
		}
		
		private function paintScrollContainer():void
		{
			var i:uint;
			for( i = 0; i < 5; i++ )
			{
				scrollA.addChild( PIXEL_ART.getLoaderBar() );
				scrollA.getChildAt( i ).x = Math.round( i * 30 );
				
				scrollB.addChild( PIXEL_ART.getLoaderBar() );
				scrollB.getChildAt( i ).x = Math.round( i * 30 );
			}
			
			scrollContainer.addChild( scrollA )
			scrollB.x = -Math.round( scrollContainer.width );
			scrollContainer.addChild( scrollB );
		}
		
		private function paintMask():void
		{
			var g:Graphics = scrollMask.graphics;
			g.beginFill( 0xff0000, 0);
			g.drawRect( 0, 0, progressViewWidth, progressViewHeight );
			g.endFill();			
		}
		
		private function paintFrame():void
		{
			progressViewVecArt = new Vector.<Vector3D>();
			
			progressViewVecArt.push( new Vector3D( 0, 0 ) );
			progressViewVecArt.push( new Vector3D( progressViewWidth, 0 ) );
			progressViewVecArt.push( new Vector3D( progressViewWidth, progressViewHeight ) );
			progressViewVecArt.push( new Vector3D( 0, progressViewHeight ) );
			
			FX.spawnFrame( frame, progressViewVecArt, progressSpawnCompleteHandler, viewData );
		}
		
		/**
		 * EVENT HANDLERS
		 */
		private function loop( e:Event ):void
		{
			scrollA.x += 5;
			scrollB.x += 5;
			
			if( scrollA.x >= scrollA.width ) scrollA.x = -Math.round( scrollA.width );
			if( scrollB.x >= scrollB.width ) scrollB.x = -Math.round( scrollB.width );
			
		}
		
		private function progressSpawnCompleteHandler():void
		{
			var g:Graphics = frame.graphics;
			g.clear();
			g.lineStyle( viewData.LINE_WEIGHT, viewData.LINE_COLOR );
			g.drawRect( 0, 0, progressViewWidth, progressViewHeight );
			
			dispatchEvent( new Event( READY ) );
		}
		
		private function FXDondeHandler():void
		{
			label.alpha = 0;
			label.text = "";
			label.x = -Math.round( label.width + 5 );
			this.removeEventListener( Event.ENTER_FRAME, loop );
		}
	}
}